package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.ResourceMapper;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleResourceMapper roleResourceMapper;

    @Override
    public PageResponse<RoleVo> findrolePage(RoleDto roleDto, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        Page<Role> rolePage = roleMapper.selectpageList(roleDto);
        PageResponse<RoleVo> response = PageResponse.of(rolePage, RoleVo.class);


        return response;
    }

    @Override
    public Boolean AddRole(RoleDto roleDto) {

        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);


        return true;
    }

    @Override
    public Set<String> findCheckedResources(Long roleId) {

        Set<String> resourcesno = roleResourceMapper.selectbyid(roleId);
        log.info("返回的数据：{}", resourcesno);

        return resourcesno;
    }

    @Override
    public void deleteByroleIds(List<String> roleIds) {
        //根据资源id删除role表的数据
        roleMapper.deleteByroleIds(roleIds);


        //根据资源id删除sys_role_resource的数据
        roleResourceMapper.deleteByroleIds(roleIds);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updatarole(RoleDto roleDto) {

        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.updateByPrimaryKey(role);
//判断checkedDeptNo为不为空
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())) {
            return true;
        }
//先删除所有的权限
        Long id = roleDto.getId();
        List<String> roleIds = Arrays.asList(id.toString());
        roleResourceMapper.deleteByroleIds(roleIds);
        String[] checkedResourceNos = roleDto.getCheckedResourceNos();

        List<RoleResource> roleResource = new ArrayList<>();

        //将字符串列表转换为RoleResource对象列表，然后批量插入
        for (String checkedResourceNo : checkedResourceNos) {
            RoleResource roleResource1 = new RoleResource(id, checkedResourceNo, SuperConstant.DATA_STATE_0);
            roleResource.add(roleResource1);

        }
        if(ObjectUtil.isEmpty(roleResource)){
            return true;
        }
        roleResourceMapper.insertBatch(roleResource);

        //再插入sys_role_resource表中的数据


        return true;
    }

    @Override
    public List<RoleVo> seletedropDownBox(RoleDto roleDto) {
      //下拉框的查询
        Role role = BeanUtil.toBean(roleDto, Role.class);
        return roleMapper.seletedropDownBox(role);



    }
}
